var prefStr;
var all_mimes = new Array();

function pow_load_prefs() {
	try {
		powBundle = document.getElementById("bundle_pow");
		document.getElementById("pow_pass_protected_checkbox").checked = pow_bc_pass_protected();
		document.getElementById("pow_loopback_only").checked = pow_bc_get_loopback_only();
		document.getElementById("port").value = pow_bc_get_port_number();
		document.getElementById("loginID").value = pow_bc_get_login();
		document.getElementById("pass").value = pow_bc_get_password();
  	document.getElementById("loginID").disabled = !pow_bc_pass_protected();
  	document.getElementById("pass").disabled = !pow_bc_pass_protected();
    document.getElementById("root_dir").value = get_pow_root_dir();
		pow_pop_mimelist();
	} catch (e) {
		log_error(e);
	}
}

function pow_do_ok() {
	try {
    pow_set_mimelist_pref(all_mimes); 
    pow_bc_set_port_pref(document.getElementById("port").value);
    pow_set_pass_protected(document.getElementById(
			"pow_pass_protected_checkbox").checked);
    pow_set_loopback_only(document.getElementById("pow_loopback_only").checked);
    pow_set_login(document.getElementById("loginID").value);
    pow_set_password(document.getElementById("pass").value);
    pow_set_root_dir(document.getElementById("root_dir").value);
    window.close();
	} catch (e) {
		log_error(e);
	}
}

function pow_add_mime() {
		try {
    var extbox = document.getElementById("extTextBox");
    var ext_name = extbox.value;
    var mimebox = document.getElementById("mimeEntry");
    var mime_name = mimebox.value;
    var binary_box = document.getElementById("binary_check");
    var is_binary = binary_box.checked;

    if(ext_name.length == 0) {
        return false;
		}

    var mime_list = document.getElementById("mime_types");
    if(! pow_list_contains_site(ext_name)) {
			var items = new Array(ext_name, mime_name, is_binary);
			pow_add_mime_row(mime_list,items);
		}
    extbox.value = "";
    mimebox.value = "";
    pow_site_input(extbox);
    extbox.focus();
		} catch (e) {
			// noop 
		}
    return true;
}

function pow_chooseRoot() {
	var nsIFilePicker = Components.interfaces.nsIFilePicker;
	var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
	fp.init(window, "Select a Root Directory", nsIFilePicker.modeGetFolder);
	var res = fp.show();
	if (res == nsIFilePicker.returnOK){
		var my_root_dir = fp.file;
		var root_dir_field = document.getElementById("root_dir");
		root_dir_field.value = my_root_dir.path;
	}
}

function pow_add_mime_row(mime_list, row) {
    var item=mime_list.appendItem(row[0]);
    all_mimes.push(row[0]);
    all_mimes.push(row[1]);
    all_mimes.push(row[2]);
    var num_rows = mime_list.getRowCount();
    item.label = row[0];
    if(num_rows > 3) {
      item.appendChild( document.createElement('listcell') ).
				setAttribute('label', row[0]);
    }
    item.appendChild( document.createElement('listcell') ).
			setAttribute('label', row[1]);
    item.appendChild( document.createElement('listcell') ).
			setAttribute('label', row[2]);
		mime_list.ensureElementIsVisible(item);
}

function pow_pop_mimelist() {
	try {
	all_mimes = new Array();
	var prefStr = pow_get_mimelist_pref();
	var i;
	var mime_list = document.getElementById("mime_types");
	if(prefStr) {
		var mimes = prefStr.split(",");
		for (i = 0; i < mimes.length; i+=3) {
				var items = new Array(mimes[i], mimes[i+1], mimes[i+2]);
				pow_add_mime_row(mime_list,items);
		}
	}
	} catch(e) {
		log_error(e);
	}
}

function pow_site_input(siteField) {
  document.getElementById("btnAdd").disabled = !siteField.value;
}

function pow_pass_protected_input(siteField) {
	try {
  	document.getElementById("loginID").disabled = !siteField.checked;
  	document.getElementById("pass").disabled = !siteField.checked;
	} catch(e) {
		log_error(e);
	}
}

function pow_site_selected(siteList) {
  document.getElementById("btnRemove").disabled = (siteList.selectedIndex == -1);
}

function pow_list_contains_site(site) {
    for(i = 0; i < all_mimes.length; i+=3) {
      if(all_mimes[i] == site) {
         return true;
			}
    }
    return false;
}

function pow_remove_all_sites() {
    var mime_list = document.getElementById("mime_types");
    while(mime_list.getRowCount() > 0) {
      mime_list.removeItemAt(0);
    }
	all_mimes = new Array();
}

function pow_remove_site() {
    var siteList = document.getElementById("mime_types");
    var index = siteList.selectedIndex;
    if(index != -1) {
			var new_mimes = new Array();
			var remove_label = siteList.getItemAtIndex(index).getAttribute('label');
      siteList.removeItemAt(index);
      document.getElementById("btnRemove").disabled = true;
			for(var i=0;i<all_mimes.length;i+=3) {
				if(all_mimes[i] != remove_label) {
					new_mimes.push(all_mimes[i]);
					new_mimes.push(all_mimes[i+1]);
					new_mimes.push(all_mimes[i+2]);
				}
			}
			all_mimes = new_mimes;
    } 
}
